#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>

/* 
 */

using namespace std;

#define NUM 1

pthread_t pt[NUM];
static int flag = 0;

void * threadRun(void * name)
{
	while (true) {
		cout << "I am other" << (char*)name << endl;
		//delete[] (char *)name;
		pthread_exit(NULL);
		sleep(1);
	}	

	return NULL;
}

int finish()
{
	cout << "finish thread start" << endl;
	for (int i = 0; i < NUM; i++) {
		char name[64];
		snprintf(name, 64, "thread-%d", i+1);
		pthread_create(pt+i, NULL, threadRun, name);	/* 因没有join，finish函数会退出，name值传不过去 */
	}

	for (int i = 0; i < NUM; i++) {
		int ret = pthread_join(pt[i], NULL);
		if (ret != 0)
			cout << "wait thread" << pt[i] << "fail" << endl;
	}
	
	/* 
	 * if have pthread_join, 先等pt[i] thread 结束，再输出 finish thread exited。
	 * if have not pthread_join, 先输出 finish thread exited,再输出 pt[i] thread 中的打印。
	 */
	cout << "finish thread exited" << endl;

	return 0;
}

int main()
{
	while (true) {
		cout << "main thread" << endl;
		finish();
		sleep(3);
	}

	return 0;
}
